/**
 * JAVACC DEMO 1.0
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.uct.common.entity.Act;
import com.apache.uct.common.entity.Sys;
import com.apache.uct.manager.ActGiveManager;
import com.apache.uct.manager.ActManager;
import com.apache.uct.manager.LockManager;

import java.util.Date;

/**
 * description:  系统注册关联信息保存
 *
 * @author iussoft 创建时间：2016-3-24
 */
public class SysRelationSavePlugin implements PluginConnector {

    private ActManager actManager;

    private ActGiveManager actGiveManager;

    private LockManager lockManager;

    /**
     * TODO 系统注册关联信息保存.
     */
    public Object execute(ParamsVo vo) throws Exception {
        String key = vo.getMethodKey();
        if ("saveInfo".equalsIgnoreCase(key)) {
            saveInfo(vo);
        } else if ("deleteInfo".equalsIgnoreCase(key)) {
            deleteInfo(vo);
        }
        return null;
    }

    private void saveInfo(ParamsVo vo) {
        Sys sys = (Sys) vo.getObj();
        Act act = new Act();
        act.setCreateUser(sys.getCreateUser());
        act.setSysEname(sys.getSysEname());
        act.setActCname(sys.getSysCname());
        act.setActEname(sys.getSysEname());
        act.setFullEname("/" + sys.getSysEname());
        act.setFullCname("/" + sys.getSysCname());
        act.setActStatus(1);
        act.setActType("menu");//默认菜单
        act.setFatherId("0");
        act.setCreateTime(new Date().getTime());
        ParamsVo<Act> actVo = new ParamsVo<Act>();
        actVo.setObj(act);
        actManager.saveInfo(actVo);
    }

    private void deleteInfo(ParamsVo vo) {
        Sys sys = (Sys) vo.getObj();
        ParamsVo pv = new ParamsVo();
        pv.setParams("sysEname", sys.getSysEname());
        pv.setParams("sysId", sys.getSysId());
        pv.setKey("BySysEname");
        //删除系统对应下所有权限
        //删除权限明细
        //删除资源 访问控制
        actManager.deleteInfo(pv);
        actGiveManager.deleteInfo(pv);
        lockManager.deleteInfo(pv);
    }

    public void setActManager(ActManager actManager) {
        this.actManager = actManager;
    }

    public void setActGiveManager(ActGiveManager actGiveManager) {
        this.actGiveManager = actGiveManager;
    }

    public void setLockManager(LockManager lockManager) {
        this.lockManager = lockManager;
    }

}
